//
//  JCDictionaryExtension.swift
//  JCPublicKit
//
//  Created by JasonLee on 2020/5/13.
//

import Foundation

extension Dictionary {
    func jc_toJsonData(prettify: Bool = false) -> Data? {
        guard JSONSerialization.isValidJSONObject(self) else {
            return nil
        }
        let options = (prettify == true) ? JSONSerialization.WritingOptions.prettyPrinted : JSONSerialization.WritingOptions()
        return try? JSONSerialization.data(withJSONObject: self, options: options)
    }
    func jc_toJsonString(prettify: Bool = false) -> String? {
        guard JSONSerialization.isValidJSONObject(self) else { return nil }
        let options = (prettify == true) ? JSONSerialization.WritingOptions.prettyPrinted : JSONSerialization.WritingOptions()
        guard let jsonData = try? JSONSerialization.data(withJSONObject: self, options: options) else { return nil }
        return String(data: jsonData, encoding: .utf8)
    }
}

func + <K, V>(lhs: [K : V], rhs: [K : V]) -> [K : V] {
    var result = [K : V]()
    for (key, value) in lhs {
        result[key] = value
    }
    for (key, value) in rhs {
        result[key] = value
    }
    return result
}
func += <K, V>( lhs: inout [K : V], rhs: [K : V]) {
    for (k, v) in rhs {
        lhs[k] = v
    }
}

